function [Y w] = tfidf2_docterm( X )
% FUNCTION applies TF-IDF weighting to document-term count matrix.
%
%   [Y w] = tfidf2( X );
%
% INPUT :
%   X        - word count vectors (one row = one document)
%            - document-term count matrix
%
% OUTPUT :
%   Y        - TF-IDF weighted document-term matrix (size(X))
%   w        - IDF weights (useful to process other documents, row vec)
%

% get inverse document frequencies
w = idf( X );

Y = sparse_diag(1 ./ sum(X,2)) * X;
Y( isnan(Y) ) = 0;      % TF
Y = Y * sparse_diag(w); % TF * IDF
end

function D = sparse_diag(d)
    D = spdiags(d(:),0,length(d),length(d));
end

% SUBFUNCTION computes word frequencies
function Y = tf( X )
% sum(X,2) = total word
Y = X * sparse_diag(1 ./ sum(X,1));

% count in each document (row vector)
Y( isnan(Y) ) = 0;                          % handle empty documents
end

% SUBFUNCTION computes inverse document frequencies
function I = idf(X)
% count the number of documents that contain a certain word
nz = sum( ( X > 0 ), 1 );

% compute idf for each document
% size(X,1) = number of document
I = log( size(X,1) ./ (nz + 1) );
end